package com.xzmzhp.controller.activity;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.activity.Program;
import com.xzmzhp.service.activity.ProgramService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-04 10:54
 * @program: ams
 * @description: 活动节目前端交互接口
 */

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/program")
public class ProgramController {
    @Autowired
    private ProgramService programService;

    /**
     * 查询所有节目数据
     * @return 节目库数据
     */
    @RequestMapping("/findAll")
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "10") int limit){
        //开启分页
        PageHelper.startPage(page, limit, true);
        List<Program> list = programService.findAll();
        if (CollUtil.isNotEmpty(list)){
            PageInfo<Program> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询节目信息失败,请重新查询");
    }

    /**
     * 根据参数查询数据
     * @param param 参数
     *        id 活动节目id
     *        programName 节目名称
     *        personnelName 人员名称
     *        societyName 社团名称
     * @return 节目信息
     */
    @RequestMapping("/findProgramByParam")
    public ReturnData findProgramByParam(@RequestParam(value = "page", defaultValue = "1") int page,
                                         @RequestParam(value = "limit", defaultValue = "10") int limit,
                                         @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<Program> list = programService.findProgramByParam(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<Program> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询节目类息失败,请重新查询");
    }

    /**
     * 根据id查询活动节目信息
     * @param id id
     * @return 活动节目信息
     */
    @RequestMapping("/findProgramById")
    public ReturnData findProgramById(@RequestParam(value = "id") Integer id){
        Program program = programService.findProgramById(id);
        if (StringUtils.isNotNull(program)) {
            return ReturnData.ok(program);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询节目信息失败,请重新查询");
        }
    }

    /**
     * 根据节目id查询活动节目信息
     * @param programId 节目id
     * @return 活动节目信息
     */
    @RequestMapping("/findProgramByProgramId")
    public ReturnData findProgramByProgramId(@RequestParam(value = "page", defaultValue = "1") int page,
                                             @RequestParam(value = "limit", defaultValue = "10") int limit,
                                             @RequestParam(value = "programId") Integer programId){
        PageHelper.startPage(page, limit, true);
        Program program = programService.findProgramById(programId);
        if (StringUtils.isNotNull(program)) {
            return ReturnData.ok(program);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询节目信息失败,请重新查询");
        }
    }

    /**
     * 添加活动节目信息
     * @param program 活动节目信息
     * @return 是否成功
     */
    @RequestMapping("/addProgram")
    public ReturnData addProgram(@RequestBody Program program){
        Integer i = programService.addProgram(program);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动节目信息添加错误");
        }
    }

    /**
     * 修改活动节目信息
     * @param program 活动节目信息
     * @return 是否成功
     */
    @RequestMapping("/exitProgram")
    public ReturnData exitProgram(@RequestBody Program program){
        Integer i = programService.exitProgram(program);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动节目信息修改错误");
        }
    }

    /**
     * 根据节目id删除活动节目信息
     * @param id 活动节目id
     * @return 是否成功
     */
    @RequestMapping("/delProgram")
    public ReturnData delProgram(Integer id){
        Integer i = programService.delProgram(id);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动节目信息删除错误");
        }
    }
}
